package com.salefinder.server.server.bd;

import java.util.List;

import com.salefinder.server.server.dao.BrandDao;
import com.salefinder.server.server.dao.ConsumerProductDao;
import com.salefinder.server.server.dao.LocationDao;
import com.salefinder.server.server.dao.MerchantDao;
import com.salefinder.server.server.dao.NotesDao;
import com.salefinder.server.server.dao.ProductDao;
import com.salefinder.server.server.dao.UserDao;
import com.salefinder.server.server.dao.UserItemListDao;
import com.salefinder.server.server.exception.DataNotFoundException;
import com.salefinder.server.server.exception.SystemException;
import com.salefinder.server.server.util.Constant;
import com.salefinder.server.server.vo.Brand;
import com.salefinder.server.server.vo.Category;
import com.salefinder.server.server.vo.ConsumerProduct;
import com.salefinder.server.server.vo.Location;
import com.salefinder.server.server.vo.Merchant;
import com.salefinder.server.server.vo.Product;
import com.salefinder.server.server.vo.User;
import com.salefinder.server.server.vo.UserItem;

public class SaleFinderBD {
	
	private static SaleFinderBD bd = null;
	
	private UserDao userDao;
	private ProductDao productDao;
	private ConsumerProductDao consumerProductDao;
	private NotesDao notesDao;
	private LocationDao locationDao;
	private UserItemListDao userItemListDao;
	private BrandDao brandDao;
	private MerchantDao merchantDao;
	
	
	private SaleFinderBD(){
		userDao = new UserDao();
		productDao = new ProductDao();
		consumerProductDao = new ConsumerProductDao();
		notesDao = new NotesDao();
		locationDao = new LocationDao();
		userItemListDao = new UserItemListDao();
		brandDao = new BrandDao();
		merchantDao = new MerchantDao();
	}
	
	public static SaleFinderBD getInstance(){
		if(bd == null){
			bd =  new SaleFinderBD();
		}
		return bd;
	}

	public void registerUser(User vo){
		userDao.createUser(vo);
	}
	
	public User login(User vo) throws DataNotFoundException, SystemException{
		try {
			return userDao.getUserByUserIdNPassword(vo);
		} catch (DataNotFoundException e) {
			throw e;
		} catch (SystemException e) {
			throw e;
		}
		
	}
	
	public List<UserItem> getMyItemList(User vo){
		User u = userDao.loadUser(vo);
		return u.getMyList();
	}
	
		
	public ConsumerProduct getConsumerProductById(ConsumerProduct vo){
		return consumerProductDao.loadConsumerProduct(vo);
	}
	
	public List<ConsumerProduct> searchCheapestConsumerProducts(ConsumerProduct vo) throws SystemException{
		return consumerProductDao.searchConsumerProduct(vo);
	}
	
	public void createConsumerProduct(ConsumerProduct vo){
		consumerProductDao.createOrUpdateConsumerProduct(vo);
	}
	
	public List<String> getProductSuggestions(String name) throws SystemException{
		return consumerProductDao.searchConsumerProductNames(name, Constant.MAX_SUGGESTION_RESULTS);
		
	}
	
	public List<Category> loadAllCategories(){
		return null;
	}
	
	public List<Brand> getBrandSuggestions(String name) throws SystemException{
		return brandDao.searchConsumerBrandNames(name, Constant.MAX_SUGGESTION_RESULTS);
	}
	
	public List<Merchant> getMerchantSuggestions(String name) throws SystemException{
		return merchantDao.searchMerchantNames(name, Constant.MAX_SUGGESTION_RESULTS);
	}
	
	public List<Location> getLocationSuggestions(String name) throws SystemException{
		return locationDao.searchLocationNames(name, Constant.MAX_SUGGESTION_RESULTS);
	}
	//**** Special functions *****//
	
	public List<ConsumerProduct> searchCheapestConsumerProduct(Product vo){
		return null;
	}

}
